﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Architecture.Data;
using HF.Framework.Web;
using HF.Framework.DataClientDBAgent;
using HF.Permission.Data;
using HF.Business.OA;

public partial class MessageList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
        //BindDDLType();
    }

    protected void lbtDetails_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('MessageAdd.aspx?state=新建','_blank')</script>");
    }

    public void BuildNoRecords(GridView gridView, DataTable dt)
    {
        try
        {
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                gridView.DataSource = dt;
                gridView.DataBind();
                int columnCount = gridView.Rows[0].Cells.Count;
                gridView.Rows[0].Cells.Clear();
                gridView.Rows[0].Cells.Add(new TableCell());
                gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
                gridView.Rows[0].Cells[0].Text = "没有数据.";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void BindData()
    {

        DataTable userTb = OA_MESSAGE.GetOA_MESSAGE(20);
        gvMessagelist.DataSource = userTb;
        gvMessagelist.DataBind();
        BuildNoRecords(gvMessagelist, userTb);
    }

    protected void gvMessagelist_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvMessagelist.DataKeys[e.RowIndex].Value.ToString();
        OA_MESSAGE.Delete(id);
        Response.Redirect(Request.RawUrl);//刷新页面

    }
    protected void gvMessagelist_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvMessagelist.PageIndex = e.NewPageIndex;
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (chkContent.Checked == false && chkTitle.Checked == false)
        {
            WorkDialog.ShowMessage(this, "请先选择查询条件!");
            return;
        }
        BindDataForSearch();
    }

    public void BindDataForSearch()
    {
        DataTable tbSer = OA_MESSAGE.GetOA_MESSAGEByKey(this.chkTitle.Checked,this.chkContent.Checked, this.txtKey.Text, this.txtContent.Text,20);
        gvMessagelist.DataSource = tbSer;
        gvMessagelist.DataBind();
        BuildNoRecords(gvMessagelist, tbSer);
    }

    //protected void BindDDLType()
    //{

    //    this.ddlType.Items.Clear();     
    //    DataSet ds = new DataSet();
    //    ds.Tables.Add(OA_MESSAGE.GetOA_MESSAGEDropList());
    //    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
    //    {
    //        foreach (DataRow dr in ds.Tables[0].Rows)
    //        {
    //            ListItem li = new ListItem();
                
    //            li.Text = dr["MsgType"].ToString();
    //            ddlType.Items.Add(li);
    //        }
    //    }
    //    this.ddlType.Items.Insert(0, new ListItem("请选择...", ""));
    //}

    public static void OpenWindowInNewPage(Page curPage, string destUrl)
    {
        string scriptString = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','_new');<", destUrl);
        scriptString += "/";
        scriptString += "script>";
        if (!curPage.IsStartupScriptRegistered("Startup"))
        {
            curPage.RegisterStartupScript("Startup", scriptString);
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string parms = (sender as LinkButton).CommandArgument.ToString();
        OpenWindowInNewPage(this.Page, @"../../Files/" + parms);

    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('MessageAdd.aspx?state=新建','_blank')</script>");
    }
}
